<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template v-slot:parent_info="{row}">
      <p v-if="row.parent" class="flex" style="align-items: flex-end;">
        <el-badge :value="row.parent.delete_time?'已注销':''">{{row.parent.nickname}}</el-badge>
      </p>
<!--      <span style="cursor: pointer" @click="to_edit({id:row.parent_id, tenant_id:row.tenant_id})" class="text-ellipsis" :title="row.parent_nickname">{{row.parent_nickname}}</span>-->
    </template>
    <template #cost="{row,col}">
      成交金额：{{row[col.name+'_over_money']}}<br>
      成交数量：{{row[col.name+'_over_num']}}
    </template>
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
      <el-button @click="to_team(row)" size="small">团队结构</el-button>
<!--      <el-button @click="show_parent_list(row)" size="small">上级序列</el-button>-->
      <el-button v-show="in_right_admin('User/log_user_change')" @click="show_log_user_change(row)" size="small">变动记录</el-button>

    </template>
  </list_url>

  <el-dialog v-model="parent_list.show" width="850px" type="0">
    <list :cols="parent_list.list_cols" :data="parent_list.list"></list>
  </el-dialog>
</template>
<script>
import {_config, _href} from "@/js/s94_tool.js";
import {iframe, in_right_admin} from "@/js/tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if (is_del){
      // if(in_right_admin('User/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      // if(in_right_admin('User/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('User/add')) btns['添加'] = function(){ iframe('user/edit') }
      if(in_right_admin('User/del')) btns['注销'] = function(){
        app.$refs['list_url'].ids_action('', 'del', false, {message:"注销后对应用户数据会被释放，无法恢复，是否确认注销？"});
      }
      if(in_right_admin('User/recycle')) btns['已注销'] = function(){ iframe('user/index?is_del=1') }
      if(in_right_admin('User/export')) btns['导出'] = function(){
        let param = s94Mjs.param_encode(app.$refs['list_url'].$refs['forms'].form_data());
        window.open(_href("/admin/user/export?"+param), '_blank')
      }
    }
    let sex_config = _config('status_alias_map.user.sex');
    let forms_config = {
      cost_time: {label:'消费统计',name:'cost_time',config:'Y-M-D',type:'date_between',add_radio:true,value:''},
      br2: {label:'',name:'',type:'br',value:''},
      add_time: {label:'注册时间',name:'where[create_time][between]',config:'Y-M-D',type:'date_between',add_radio:true,value:'',br:true},
      keyword: {label:'昵称/电话',name:'where[nickname|phone][like]',type:'text_one',value:''},
      id: {label:'用户ID',name:'where[id][eq]',type:'text_one',value:'',br:true},
      parent_keyword: {label:'上级用户',name:'parent_keyword',type:'text_one',value:''},
      del_parent: {label:'已注销上级',name:'del_parent',type:'radio',config:'全部|,是|1,否|0',button:1,value:'',br:true},
      money_between: {label:'余额',name:'money_between',type:'text_between',value:''},
      money_time: {label:'指定时间余额',name:'money_time',type:'date',config:'Y-M-D',value:'',br:true},
      territory_area: {label:'代理区域',name:'territory_area_title',type:'text_one',value:''},
      level: {label:'等级',name:'where[level][eq]',type:'select_one',config:[],value:'',br:true},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left'},
      // encode_id:{name:'encode_id', label:'用户编号', width:'80px'},
      head_img:{name:'head_img', label:'头像', width:'45px', type:'file_img'},
      phone:{name:'phone', label:'电话号码', width:'120px'},
      nickname:{name:'nickname', label:'昵称', width:'120px'},
      level:{name:'level', label:'等级', width:'120px',type:'select_one',config:[]},
      parent:{name:'parent', label:'上级用户', width:'120px', type:'parent_info'},
      sex:{name:'sex', label:'性别', width:'40px',type:'select_one',config:sex_config},
      // area_str:{name:'area_str', label:'所在区域', min_width:'200px'},
      money:{name:'money', label:'余额', width:'75px'},
      input_time_money:{name:'input_time_money', label:'指定时间余额', width:'75px'},
      territory_area_title:{name:'territory_area_title', label:'代理区域',type:'text_more', width:'100px'},
      sum_recharge:{name:'sum_recharge', label:'累计充值', width:'80px'},
      input_time_cost:{name:'input_time_cost', label:'筛选时间消费情况', type:'cost', width:'150px',align:'left'},
      month_cost:{name:'month_cost', label:'本月消费情况', type:'cost', width:'150px',align:'left'},
      last_month_cost:{name:'last_month_cost', label:'上月消费情况', type:'cost', width:'150px',align:'left'},
      all_cost:{name:'all_cost', label:'累计消费情况', type:'cost', width:'150px',align:'left'},
      last_time:{name:'last_time', label:'最后登录时间', width:'165px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'注销时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'注册时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'140px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/user/'+(is_del?'recycle':'list'),
        title: "用户列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          let cost_time = app.config.forms_config.cost_time.value;
          let col_label = cost_time ? cost_time.replace(',','至')+'消费情况' : '筛选时间消费情况';
          app.config.list_cols.input_time_cost.label = col_label;

          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },

      parent_list: {
        show: false,
        list_cols: {
          layer:{name:'layer', label:'层级', width:'100px', fixed:'left'},
          id: {name:'id', label:'ID', width:'65px', fixed:'left'},
          head_img:{name:'head_img', label:'头像', width:'45px', type:'file_img'},
          phone:{name:'phone', label:'电话号码', width:'120px'},
          nickname:{name:'nickname', label:'昵称', width:'120px'},
          level:{name:'level', label:'等级', width:'120px',type:'select_one',config:[]},
          // money:{name:'money', label:'余额', width:'75px'},
          delete_time:{name:'delete_time', label:'注销时间', width:'165px', type:'datetime'},
        },
        list: []
      }
    }
  },
  methods: {
    in_right_admin: in_right_admin,
    to_edit(row){
      let can_edit = in_right_admin('User/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('user/edit?'+s94Mjs.param_encode(param))
    },
    to_team(row){
      iframe({href:`user/team?id=${row.id}`, title: row.nickname+'的团队结构'});
    },
    show_log_user_change(row){
      iframe('user/log_user_change?id='+row.id);
    },
    show_parent_list(row){
      this.parent_list.list = [];
      this.parent_list.show = true;
      ajax({url:'/admin/user/parent_list?id='+row.id,cache:true},(res)=>{
        for (let i = 0; i < res.data.list.length; i++) {
          res.data.list[i]['layer'] = `第${i+1}上级`;
        }
        this.parent_list.list = res.data.list;
        return false;
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    ajax({url:'/admin/user_level/list?order[id]=asc',cache:true},(res)=>{
      let level_config = [];
      res.data.list.forEach((row)=>{
        level_config.push({content:`(${row.level})${row.name}`, value:row.level});
      })
      this.config.forms_config.level.config = level_config;
      this.config.list_cols.level.config = level_config;
      this.parent_list.list_cols.level.config = level_config;
      return false;
    })
  }
}
</script>
